package com.vince.model.service;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;

import com.vince.model.domain.User;

/**  
 * 
 * struts2：重写文件上传和分页
 * 面向对象的思想
 * 
 * 分页类
 *  页总数 pageCount
 *  总记录条数 rowCount
 *  每页显示的条数 pageRowCount/pageSize
 *  当前页 curPage
 *  得到的数据 data
 *  
 *   
 *   
 *   
 * @author susp
 *
 */
public class Paging {
	
	private int pageCount;//总页数
	private int rowCount;//总条数
	private int pageSize;//每页条数
	private Collection data;//要显示的数据
	private static int curPage=1;//当前页
	
	
	//默认每页显示10条记录
	public Paging(Collection data){
		this.data=data;
		this.pageSize=10;
		this.rowCount=data.size();
		this.pageCount=(int) Math.ceil(this.rowCount/this.pageSize);//向上取整
		
	}
	
	//自行设置每页显示的条数
	public Paging(Collection data,int pageSize){
		this.data=data;
		this.rowCount=data.size();
		this.pageSize=pageSize;
		this.pageCount=(int) Math.ceil(this.rowCount/this.pageSize);//总页数
		
	}

	public void setFirst(){
		this.curPage=1;
	}
	
	public void setLast(){
		this.curPage=this.pageCount;
	}

	public int getCurPage(){
		return this.curPage;
	}
	public void setCurPage(int num){
		this.curPage=num;
	}
	public int getPageCount(){
		return this.pageCount;
	}
	//上一页
	public void previous(){
		if(this.curPage==1){
			this.curPage=1;
		}else if(this.curPage>1){
			this.curPage-=1;
		}
		
	}
	
	//下一页
	public void next(){
		if(this.curPage<this.pageCount){
			this.curPage+=1;
		}else if(this.curPage==this.pageCount){
			this.curPage=this.pageCount;
		}
	}
	
	//根据curPage参数获取当前页要展示的数据
	public Collection getPageData(){
		Collection curdata=null;
		//curPage,PageSize
		if(this.data!=null){
			int start,end;
			start=(this.curPage-1)*this.pageSize;
			if(this.curPage*this.pageSize>this.rowCount){
				//当前页为满
				end=this.rowCount;//rowCount从1开始
			}else{
				end=start+this.pageSize-1;
				
			}
			System.out.println();
			ArrayList arrayData = null;  
            Vector vectorData = null;  
            ArrayList arrayCurData = new ArrayList();  
            Vector vectorCurData = new Vector();  
            boolean isArrayData = true;  
            //判断是属于哪一种集合  
            if(data instanceof ArrayList) {  
                arrayData = (ArrayList)data;  
                isArrayData = true;  
            } else if(data instanceof Vector) {  
                vectorData = (Vector)data;  
                isArrayData = false;  
            }  
            //循环得到每一页的数据  
            for(int i = start; i <=end; i++) {  
                if(isArrayData) {  
                    arrayCurData.add(arrayData.get(i));  
                } else {  
                    vectorCurData.add(vectorData.get(i));  
                }  
            }  
              
            if(isArrayData) {  
                curdata = arrayCurData;  
            } else {  
                curdata = vectorCurData;  
            }  
		
		}

		return curdata;
	}
	

	
	
}

